import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/common/shop_material.dart';
import 'package:shop/utils/Storage.dart';

import 'common/BaseError.dart';
import 'common/Provider.dart';
import 'routers/Router.dart';

//入口方法
//by: yrs
Future<Null> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FlutterError.onError = (FlutterErrorDetails details) {
    Zone.current.handleUncaughtError(details.exception, details.stack);
  };

  runZonedGuarded(() async {
    StorageUtil.sp = await SharedPreferences.getInstance();
    runApp(new MyApp());
  }, (error, stackTrace) => BaseError(error, stackTrace));
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    // ErrorWidget.builder =
    //     (details) => ErrorPage.getErrorWitget(context, details);
    return MultiProvider(
      providers: providers,
      child: MaterialApp(
        showPerformanceOverlay: false,
        debugShowCheckedModeBanner: false,
        initialRoute: MyRoutes.guidePage,
        onGenerateRoute: MyRoutes.generateRoute,
        theme: ThemeData(primaryColor: Colors.white),
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        supportedLocales: [const Locale('zh', 'CH'), const Locale('en', 'US')],
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (AppLifecycleState.resumed == state) {}
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}
